package com.studio.dao.impl;

import java.util.List;

import com.studio.base.dao.BaseDao;
import com.studio.base.utils.Pager;
import com.studio.dao.UsersDao;
import com.studio.entity.Users;

public class UsersDaoImpl extends BaseDao implements UsersDao {

	@Override
	public boolean add(Users user) {
		Integer i = (Integer)this.getHibernateTemplate().save(user);
		return i>0;
	}

	@Override
	public void del(Users user) {
		this.getHibernateTemplate().delete(user);
	}

	@Override
	public Pager find(Pager pager) {
		return findPageByCriteria(pager.getPageNo(), pager.getPageSize(), new Users());
	}

	@Override
	public void selectById() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Users findById(Integer id) {
		Users user = new Users();
		List<?> list =  this.getHibernateTemplate().find("from Users where id = "+id);
		if(list!=null && list.size()>0){
			user = (Users)list.get(0);
		}
		return user;
	}

	@Override
	public Users findByExample(Users user) {
		List<?> list = this.getHibernateTemplate().findByExample(user,0,1);
		user= null;
		if(list!=null && list.size()>0){
			user = (Users)list.get(0);
		}
		return user;
	}

	@Override
	public void delByIds(Integer[] ids) {
		for (Integer id : ids) {
			String hqlDelete = "delete Users u where u.id = :id";
			getSession().createQuery( hqlDelete )
			.setInteger( "id", id )
			.executeUpdate();
		}
	}

	@Override
	public void edit(Users user) {
		this.getHibernateTemplate().update(user);
	}

	@Override
	public List<?> findGroupBy(String key) {
		return this.getHibernateTemplate().find("select "+key+",count(*) from Users group by "+key);
	}

}
